/*
 * @Author: zhangxuanjiu 
 * @Date: 2017-08-31 10:29:41 
 * @Last Modified by: zhangxuanjiu
 * @Last Modified time: 2018-04-04 09:40:54
 */

const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin')

var webpackConfig = {
  resolve: {
    modules: [
      path.join(__dirname, 'src'),
      'node_modules'
    ],
    alias: {
      components: path.join(__dirname, './src/components'),
      game: path.join(__dirname, './src/game'),
      src: path.join(__dirname, './src')
    },
    extensions: ['.js', '.css', '.less', '.ts', '.tsx'],
  },
  context: path.join(__dirname, 'src'),
  entry: {
    app: [
      'babel-polyfill',
      'whatwg-fetch',
      './lib/hammer.min.js',
      './lib/lodash_lib.min.js',
      './Main.tsx'
    ]
  },
  output: {
    path: path.join(__dirname, 'dist'),
    filename: '[name].[hash:5].js',
    publicPath: './',
    chunkFilename: '[name].js?[hash:8]'
  },
  plugins: [
    new webpack.DefinePlugin({
      env: JSON.stringify('build'),
      imageStorageVersion: JSON.stringify(328),
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    // new webpack.optimize.UglifyJsPlugin({
    //   mangle: true,
    //   compress: {
    //     warnings: false, // 禁止生成warning
    //     drop_debugger: true,
    //     drop_console: true
    //   }
    // }),
    new ParallelUglifyPlugin({
      cacheDir: 'src/.cache/',
      uglifyJS: {
        mangle: true,
        output: {
          comments: false
        },
        compress: {
          warnings: false,
          drop_debugger: true,
          drop_console: true
        }
      }
    }),
    new HtmlWebpackPlugin({
      // title: 'rd平台',
      template: './index.html', // 源模板文件
      filename: './index.html', // 输出文件【注意：这里的根路径是module.exports.output.path】
      showErrors: true,
      inject: 'body',
      hash: true
      // chunks: ["common",'index']
    })
  ],
  module: {
    rules: [
      {
        test: /\.js$/,
        use: ['babel-loader'],
        exclude: /node_modules/
      },
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.css/,
        use: ['style-loader', 'css-loader', 'postcss-loader'],
      },
      {
        test: /\.less/,
        use: ['style-loader', 'css-loader', 'postcss-loader', 'less-loader']
      },
      {
        test: /\.(png|jpg|gif|svg|eot|svg|ttf|woff|woff2)$/,
        loader: 'file-loader',
        options: {
          name: 'img/styleImg/[name].[ext]'
        }
      }
    ],
  }
}

module.exports = webpackConfig